<?php

class CategoryComponent extends Category {
	
	public static function getList($filter=array()) {
        $model = self::model()->findAllByAttributes(array('parent'=>0),array('condition'=>'id<>'.$filter['id']));
		
		$categoryList = array(""=>Yii::t('common', '--Choose--'));
        foreach ($model as  $obj) {
            $categoryList[$obj->id] = $obj->name;
        }
        return $categoryList;
    }
	
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'name' => Yii::t('category', 'Name'),
			'parent' => 'Parent',
			'description' => 'Description',
		);
	}
	
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
            'categoryParent' => array(self::BELONGS_TO, 'CategoryComponent', 'parent'),
		);
	}
    
    public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('name',$this->name,true);
        ///echo $this->name;die('aaaaaaaaaaa');
        $criteria->with='categoryParent'; 
		$criteria->compare('categoryParent.name',$this->categoryParent,true);  
       
		$criteria->compare('description',$this->description,true);
        
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
    
    public function canDelete($id) {
        return ($this->count(array('condition'=>'parent='.$id))>0)?false:true;
    }
}
	